//
//  WWFreshSellCell.m
//  Shopping
//
//  Created by 孙博 on 16/8/20.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWFreshSellCell.h"
#import "WWFreshCollCell.h"

#define kMargin 10
#define kWidth  self.contentView.frame.size.width
#define kHeight self.contentView.frame.size.height

@interface WWFreshSellCell ()<UICollectionViewDataSource, UICollectionViewDelegate >

@property (nonatomic, weak)UICollectionView *collection;
@property (nonatomic, strong)UICollectionViewFlowLayout *flow;
@end

@implementation WWFreshSellCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

#pragma
#pragma mark - coll 数据源 -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WWFreshCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"aa" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.models[indexPath.item];
    
    return cell;
}

#pragma
#pragma mark - coll 代理 -
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击cell");
    WWFreshSellModel *model  = self.models[indexPath.item];
    [[NSNotificationCenter defaultCenter]postNotificationName:GoodsDetailsNotiName object:nil userInfo:@{(WWFreshSellModel *)model : model}];
    
    if (self.backBlock) {
        self.backBlock();
    }
}

#pragma
#pragma mark - 设置UI -
- (void)setupUI{
    self.flow = [UICollectionViewFlowLayout new];
    
    UICollectionView *collView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flow];
    collView.dataSource = self;
    collView.delegate = self;
    self.collection = collView;
    [collView registerClass:[WWFreshCollCell class] forCellWithReuseIdentifier:@"aa"];
    collView.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:collView];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.collection.frame = self.contentView.frame;

    CGFloat width = (kWidth - kMargin) * 0.5;
    _flow.itemSize = CGSizeMake(width, kHeight);
    _flow.minimumInteritemSpacing = kMargin;
}

// MARK : - 设置cell的frame -
- (void)setFrame:(CGRect)frame{
    
    frame.size.height -= 8;
    frame.origin.y  += 8;
    frame.origin.x +=8;
    frame.size.width -=16;
    [super setFrame:frame];
}
@end
